package com.aistar.controller;

import com.aistar.model.bo.Result;
import com.aistar.model.entity.Employee;
import com.aistar.model.entity.Employer;
import com.aistar.model.entity.User;
import com.aistar.service.user.IEmployeeInfoService;
import com.aistar.service.user.ILoginService;
import com.aistar.service.user.IUserService;
import com.aistar.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: LoginRestController
 * @author: LiuWenXiu
 * @date: 2021/10/15 19:10
 * @Description: TODO...本类用来演示: 登录功能控制层
 */
@RestController
@RequestMapping("/login")
public class LoginRestController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IEmployeeInfoService employeeInfoService;

    @PostMapping("/pwd")
    public Result login(HttpServletRequest request, String username, String password) {
//        System.out.println(username);
//        System.out.println(password);

        // 首先验证用户的输入
        if (null == username || username.trim().length() == 0)
            return new Result("501", "用户名不能为空");
        if (null == password || password.trim().length() == 0)
            return new Result("502", "密码不能为空");

        // 验证用户账号密码
        User user = loginService.judgeLogin(username, password);
        // 如果user为空,表示验证失败
        if (null == user)
            return new Result("503", "用户名或密码错误,请重试");
        // 到这一步表示验证成功了

        // 把用户的信息放到token里
        String token = JWTUtils.geneJsonWebToken(user);

        //Todo... 申请session资源
        HttpSession session = request.getSession();

//        System.out.println(user);
        // 需要进一步进行判断 - 判断用户是求职者还是招聘者
        if (user.getUserType().equals("求职者")) {
            // 表示是求职者
//            System.out.println("求职者");

            // ToDo... 暂时把求职者信息放到session里
            Employee employee = loginService.loadEmployeeByUserId(user.getId());
            session.setAttribute("employee", employee);
            return new Result("201", "求职者登录成功", token);
        } else {
            // 表示是招聘者
            //            System.out.println("招聘者");
            //ToDo... 暂时把招聘者信息放到session里
            Employer employer = loginService.loadEmployerByUserId(user.getId());
            session.setAttribute("employer", employer);
            return new Result("202", "招聘者登录成功", token);
        }
    }


    @PostMapping("/email/code")
    public Result emailCode(String email) {
        Integer count = loginService.sendEmailCode(email);
        if (count == -1)
            return new Result("501", "邮箱不存在");
        else if (count == 1)
            return new Result("200", "验证码发送成功");
        return new Result("500", "很抱歉,请您重试");
    }


    @PostMapping("/email")
    public Result Email(HttpServletRequest request, String email, String code) {
        if (null == email || email.length() == 0)
            return new Result("501", "邮箱不能为空");
        if (null == code || code.length() == 0)
            return new Result("502", "验证码不能为空");
        Integer res = loginService.judgeLoginByEmail(email, code);
        if (res == -1)
            return new Result("503", "邮箱不存在");
        if (res == -2)
            return new Result("504", "验证码已过期");
        if (res == 5 || res == 10) {
            // 表示验证成功 需要判断是求职者还是招聘者
            User user = userService.findByEmail(email);
            // 把用户的信息放到token里
            String token = JWTUtils.geneJsonWebToken(user);

            //Todo... 申请session资源
            HttpSession session = request.getSession();
            if (res == 10) {
                Employer employer = loginService.loadEmployerByUserId(user.getId());
                session.setAttribute("employer", employer);
                return new Result("202", "招聘者验证成功", token);
            }

            System.out.println("我的求职者");
            Employee employee = loginService.loadEmployeeByUserId(user.getId());
            session.setAttribute("employee", employee);
            return new Result("201", "求职者验证成功", token);
        }

        if (res == 0)
            return new Result("203", "验证码错误");
        return new Result("505", "很抱歉,请重试");
    }


}
